package com.feng.seata.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feng.seata.mapper.UserEntityMapper;
import com.feng.seata.service.UserService;
import com.feng.seata.model.UserEntity;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.stereotype.Service;

/**
 * @author yinglong.zhang
 * @description
 * @date 2024-1-19 13:22
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserEntityMapper, UserEntity> implements UserService {

    @Override
    public Long addUser(UserEntity userEntity) {
        boolean save = this.save(userEntity);
        return save ? userEntity.getId() : -1L;
    }

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void addScore(UserEntity userEntity) {
        UserEntity user = lambdaQuery().eq(UserEntity::getId, userEntity.getId()).one();
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        lambdaUpdate().set(UserEntity::getScore, user.getScore() + userEntity.getScore())
                .eq(UserEntity::getId, userEntity.getId()).update();
    }
}
